/**********************************************************
*
*	config.c
*
*  Copyright 2007,2008 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*  This module provides functions which read configuration
*    parameters from the command line and config file.
*
**************************************************************************
*
*  version 1.0.5, 10/4/08, SBS
*    - documentation
*
*  version 1.0.4, 6/4/08, SBS
*    - change to readconfig_str2
*
*  version 1.0.3, 4/1/08, SBS
*    - minor cleanup / documentation
*
*  version 1.0.2, 12/07/07, SBS
*    - add log file name to config file/data
*
*  version 1.0.1, 11/30/07, SBS
*    - add logfile output
*
*  version 1.0.a, 5/8/07, SBS
*    - irc_client-specific version
*
*	version 1.0, 4/5/07, SBS
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @file 
*  @ingroup common
*  @brief
*  This module provides functions which read configuration
*    parameters from the command line and config file.
*  <hr>
**/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <string.h>

#include "readconfigfile.h"
#include "logfile.h"

#include "config.h"

#define MAXPATHLENGTH 512

/************************************************
*  Default configuration values
************************************************/

#define DEFSENDCOMMAND "scp -p -i"
#define DEFRETRCOMMAND "wget -N"
#define DEFIDENTITYKEY "taosfscpkey"
#define DEFTIMEPERIOD 1.01
#define DEFNUMFILES 1

#define DEFREPORTPERIOD 1


#define DEFUSERNAME "taosfscponly"
#define DEFSERVERNAME "gs4910.sp.cs.cmu.edu"

#define DEFCMDFILEBASE "command"
#define DEFCMDFILEEXT "txt"

#define DEFTELEMFILEBASE "telemetry"
#define DEFTELEMFILEEXT "txt"

#define DEFREMOTEPATH "taosf/test/"

#define DEFNUMROBOTS 1
#define DEFDOCOMMAND 0

#define DEFJAVAUSERDIR "C:\\code\\asf\\project"
#define DEFJAVACLASSPATH ".;C:\\code\\asf\\project;C:\\code\\irc;C:\\code\\asf\\project\\classes;C:\\code\\asf\\project\\lib\\asf.jar;C:\\code\\asf\\project\\examples"

#define DEFIRCRESOURCE "-Presources/ocean/config/ssa/ssa.plist"

#define DEFMOCUDIR "e:\\devel\\ssc-sd\\Mocu\\"
#define DEFARCHIVEDIR "/var/www/taosf/archive"

#define DEFDBSERVER "localhost"
#define DEFDBNAME "taosf"
#define DEFDBUSER "taosf"
#define DEFDBPWD  "foo"

#define DEFLOGFILENAME "logfile.log"


/********************************************************
*
*  readcommandline()
*  
*  Uses getopt() to read command line options
*
*  Parameters:
*    argc, argv - arguments from main()
*
*    configfilespec - if the -f option is used the 
*       corresponding filename will be copied into 
*		  this variable
*
*  Returns:
*    none
*
********************************************************/
static void readcommandline(int argc, char *argv[], char *configfilespec)
{
	int opt; 
	
	configfilespec[0] = '\0';

	while ((opt = getopt(argc, argv, "f:nh")) > 0) {
		switch (opt) {
			case 'f':	// filespec
				printf("===SSA=== Command line option \"-f %s\" found.\n",optarg);
				snprintf(configfilespec,MAXPATHLENGTH,optarg);
				printf("===SSA=== Using config file '%s'\n\n",configfilespec);				
				break;
						
			case 'h':	// help
			case '?':
			default:
				// print usage instructions and exit
				printf("\n===SSA=== usage: %s [options]\n",argv[0]);
				printf("===SSA===   -f <filespec>\tUse config file <filespec>.\n\n");
				exit(-1);
		}
	}

	return;
}


/***********************************************
*
*  config_readconfig()
*
*   Reads configuration data from a file
*
*   The name of the config file is read from the command line (-f option)
*
*   If there is no cmd line arg, then a default filename is used.
*
*
* Parameters:
*   argc, argv - arguments from main() 
*
*   defcfgfilename - the config file to use if none given on command line
*
*
*  Returns:
*    none
*
***********************************************/
CONFIGDATA config_readconfig( int argc, char *argv[], char *defcfgfilename)
{
	CONFIGDATA cf;	
	char configfilespec[MAXPATHLENGTH];
	
	readcommandline(argc, argv, configfilespec);
	
	if(strlen(configfilespec) == 0){
		snprintf(configfilespec,sizeof(configfilespec),defcfgfilename);

		#ifdef DEBUG		
		printf("===SSA=== Using config file '%s'\n",configfilespec);
		#endif		
	}

	
	readconfig_str2(
				cf.retrcommand
				,sizeof(cf.sendcommand)
				,configfilespec
				,"general"
				,"retrcommand"
				,DEFRETRCOMMAND);
//	strncpy(cf.retrcommand
//				,readconfig_str(configfilespec,"general","retrcommand",DEFRETRCOMMAND)
//				,sizeof(cf.retrcommand));
//
	readconfig_str2(
				cf.sendcommand
				,sizeof(cf.sendcommand)
				,configfilespec
				,"general"
				,"sendcommand"
				,DEFSENDCOMMAND);
//	strncpy(cf.sendcommand
//				,readconfig_str(configfilespec,"general","sendcommand",DEFSENDCOMMAND)
//				,sizeof(cf.sendcommand));
//
	readconfig_str2(
				cf.identitykey
				,sizeof(cf.identitykey)
				,configfilespec
				,"general"
				,"identitykey"
				,DEFIDENTITYKEY);

//	strncpy(cf.identitykey
//				,readconfig_str(configfilespec,"general","identitykey",DEFIDENTITYKEY)
//				,sizeof(cf.identitykey));
//
	cf.numrobots = readconfig_float(
				configfilespec
				,"general"
				,"numrobots"
				,DEFNUMROBOTS);

	cf.docommand = readconfig_int(
				configfilespec
				,"general"
				,"docommand"
				,DEFDOCOMMAND);

	cf.reportperiod = readconfig_int(
				configfilespec
				,"general"
				,"reportperiod"
				,DEFREPORTPERIOD);

	readconfig_str2(
				cf.servername
				,sizeof(cf.servername)
				,configfilespec
				,"remotehost"
				,"servername"
				,DEFSERVERNAME);

//	strncpy(cf.servername
//				,readconfig_str(configfilespec,"remotehost","servername",DEFSERVERNAME)
//				,sizeof(cf.servername));

	readconfig_str2(
				cf.username
				,sizeof(cf.username)
				,configfilespec
				,"remotehost"
				,"username"
				,DEFUSERNAME);

//	strncpy(cf.username
//				,readconfig_str(configfilespec,"remotehost","username",DEFUSERNAME)
//				,sizeof(cf.username));

	readconfig_str2(
				cf.remotepath
				,sizeof(cf.remotepath)
				,configfilespec
				,"remotehost"
				,"remotepath"
				,DEFREMOTEPATH);

//	strncpy(cf.remotepath
//				,readconfig_str(configfilespec,"remotehost","remotepath",DEFREMOTEPATH)
//				,sizeof(cf.remotepath));

	readconfig_str2(
				cf.cmdfilebase
				,sizeof(cf.cmdfilebase)
				,configfilespec
				,"file"
				,"cmdfilebase"
				,DEFCMDFILEBASE);

//	strncpy(cf.cmdfilebase
//				,readconfig_str(configfilespec,"file","cmdfilebase",DEFCMDFILEBASE)
//				,sizeof(cf.cmdfilebase));
			
	readconfig_str2(
				cf.cmdfileext
				,sizeof(cf.cmdfileext)
				,configfilespec
				,"file"
				,"cmdfileext"
				,DEFCMDFILEEXT);

//	strncpy(cf.cmdfileext
//				,readconfig_str(configfilespec,"file","cmdfileext",DEFCMDFILEEXT)
//				,sizeof(cf.cmdfileext));
	
	readconfig_str2(
				cf.telemfilebase
				,sizeof(cf.telemfilebase)
				,configfilespec
				,"file"
				,"telemfilebase"
				,DEFTELEMFILEBASE);

//	strncpy(cf.telemfilebase
//				,readconfig_str(configfilespec,"file","telemfilebase",DEFTELEMFILEBASE)
//				,sizeof(cf.telemfilebase));
			
	readconfig_str2(
				cf.telemfileext
				,sizeof(cf.telemfileext)
				,configfilespec
				,"file"
				,"telemfileext"
				,DEFTELEMFILEEXT);

//	strncpy(cf.telemfileext
//				,readconfig_str(configfilespec,"file","telemfileext",DEFTELEMFILEEXT)
//				,sizeof(cf.telemfileext));
	
	readconfig_str2(
				cf.javauserdir
				,sizeof(cf.javauserdir)
				,configfilespec
				,"java"
				,"userdir"
				,DEFJAVAUSERDIR);

//	strncpy(cf.javauserdir
//				,readconfig_str(configfilespec,"java","userdir",DEFJAVAUSERDIR)
//				,sizeof(cf.javauserdir));
	
	readconfig_str2(
				cf.javaclasspath
				,sizeof(cf.javaclasspath)
				,configfilespec
				,"java"
				,"classpath"
				,DEFJAVACLASSPATH);

//	strncpy(cf.javaclasspath
//				,readconfig_str(configfilespec,"java","classpath",DEFJAVACLASSPATH)
//				,sizeof(cf.javaclasspath));
	
	readconfig_str2(
				cf.ircresource
				,sizeof(cf.ircresource)
				,configfilespec
				,"irc"
				,"resource"
				,DEFIRCRESOURCE);

//	strncpy(cf.ircresource
//				,readconfig_str(configfilespec,"irc","resource",DEFIRCRESOURCE)
//				,sizeof(cf.ircresource));

	readconfig_str2(
				cf.mocudir
				,sizeof(cf.mocudir)
				,configfilespec
				,"file"
				,"mocudirectory"
				,DEFMOCUDIR);

//	strncpy(cf.mocudir
//				,readconfig_str(configfilespec,"file","mocudirectory",DEFMOCUDIR)
//				,sizeof(cf.mocudir));

	readconfig_str2(
				cf.archivedir
				,sizeof(cf.archivedir)
				,configfilespec
				,"file"
				,"archivedir"
				,DEFARCHIVEDIR);
				
//	strncpy(cf.archivedir
//				,readconfig_str(configfilespec,"file","archivedir",DEFARCHIVEDIR)
//				,sizeof(cf.archivedir));

	readconfig_str2(
				cf.dbusername
				,sizeof(cf.dbusername)
				,configfilespec
				,"database"
				,"username"
				,DEFDBUSER);
				
//	strncpy(cf.dbusername
//				,readconfig_str(configfilespec,"database","username",DEFDBUSER)
//				,sizeof(cf.dbusername));

	readconfig_str2(
				cf.dbdatabasename
				,sizeof(cf.dbdatabasename)
				,configfilespec
				,"database"
				,"name"
				,DEFDBNAME);
				
//	strncpy(cf.dbdatabasename
//				,readconfig_str(configfilespec,"database","name",DEFDBNAME)
//				,sizeof(cf.dbdatabasename));

	readconfig_str2(
				cf.dbservername
				,sizeof(cf.dbservername)
				,configfilespec
				,"database"
				,"servername"
				,DEFDBSERVER);
				
//	strncpy(cf.dbservername
//				,readconfig_str(configfilespec,"database","servername",DEFDBSERVER)
//				,sizeof(cf.dbservername));

	readconfig_str2(
				cf.dbuserpasswd
				,sizeof(cf.dbuserpasswd)
				,configfilespec
				,"database"
				,"password"
				,DEFDBPWD);
				
//	strncpy(cf.dbuserpasswd
//				,readconfig_str(configfilespec,"database","password",DEFDBPWD)
//				,sizeof(cf.dbuserpasswd));

	readconfig_str2(
				cf.logfilename
				,sizeof(cf.logfilename)
				,configfilespec
				,"general"
				,"logfile"
				,DEFLOGFILENAME);
				
//	strncpy(cf.logfilename
//				,readconfig_str(configfilespec,"general","logfile",DEFLOGFILENAME)
//				,sizeof(cf.logfilename));

	return(cf);
}


/***********************************************
*
*  config_PrintConfig()
*
*  prints a CONFIGDATA struct to the log file
*
*  Parameters:
*    cf - the struct
*
*  Returns:
*    none
*
***********************************************/
void config_PrintConfig(CONFIGDATA cf)
{
	#ifdef DEBUG
	char temp[LOGSTRLEN];
	
	snprintf(temp,LOGSTRLEN,"send command = %s",cf.sendcommand);
	logfile_LogPrint(temp);
	
	snprintf(temp,LOGSTRLEN,"retr command = %s",cf.retrcommand);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"identity file = %s",cf.identitykey);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"numrobots = %d",cf.numrobots);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"docommand = %d",cf.docommand);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"servername = %s",cf.servername);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"username = %s",cf.username);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"remote path = %s",cf.remotepath);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"cmdfilebase = %s",cf.cmdfilebase);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"cmdfileext = %s",cf.cmdfileext);
	logfile_LogPrint(temp);
	
	snprintf(temp,LOGSTRLEN,"telemfilebase = %s",cf.telemfilebase);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"telemfileext = %s",cf.telemfileext);
	logfile_LogPrint(temp);
	
	snprintf(temp,LOGSTRLEN,"javauserdir = %s",cf.javauserdir);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"javaclasspath = %s",cf.javaclasspath);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"ircresource = %s",cf.ircresource);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"mocu directory = %s",cf.mocudir);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"archive directory = %s",cf.archivedir);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"database");
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"   server = %s",cf.dbservername);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"   database = %s",cf.dbdatabasename);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"   user = %s",cf.dbusername);
	logfile_LogPrint(temp);

	snprintf(temp,LOGSTRLEN,"   password = %s",cf.dbuserpasswd);
	logfile_LogPrint(temp);
	
	logfile_LogPrint(" ");
	#endif

	return;
}



